<?php
class HcThuocsController extends AppController {

	var $name = 'HcThuocs';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->HcThuoc->recursive = 0;
		$this->set('hcThuocs', $this->paginate());
	}

	function index1($id=null) {
//		$this->Cookie->write('old',0,false,1000);
		
		if (!$id) {
			$this->Session->setFlash('Thao tác không hợp lệ');
			$this->redirect(array('controller'=>'Thuocs','action'=>'index'));
		}
		$t = $this->HcThuoc->Thuoc->find('first', array('conditions'=>array('Thuoc.id'=>$id)));
		$this->HcThuoc->recursive = 1;
		$this->set('hcThuocs', $this->paginate());
		
		$this->HcThuoc->recursive = 1;
	
		
		$hcThuocs = $this->paginate('HcThuoc',array('HcThuoc.thuoc_id'=>$id));
			//print_r($dtLoaidts);						
		$this->set('hcThuocs', $hcThuocs);

		$tuy = $this->HcThuoc->Thuoc->find('first', array('conditions'=>array('Thuoc.id'=>$id)));	
	
              
		$this->set(compact('t','tuy','id'));
	}


	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid HcThuoc.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('hcThuoc', $this->HcThuoc->read(null, $id));
	}

//	function add() {
//		if (!empty($this->data)) {
//			$this->HcThuoc->create();
//			if ($this->HcThuoc->save($this->data)) {
//				$this->Session->setFlash(__('The HcThuoc has been saved', true));
//				$this->redirect(array('action'=>'index'));
//			} else {
//				$this->Session->setFlash(__('The HcThuoc could not be saved. Please, try again.', true));
//			}
//		}
//		$hoatchats = $this->HcThuoc->Hoatchat->find('list');
//		$thuocs = $this->HcThuoc->Thuoc->find('list');
//		$this->set(compact('hoatchats', 'thuocs'));
//	}


	function add($dtid = null) {
		
//		if (!$this->Cookie->read('old')) {
		if (!$dtid) {
			$this->Session->setFlash('Thao tác không hợp lệ');
			$this->redirect(array('controller' => 'thuocs', 'action'=>'index'));
		}
		
		if (!empty($this->data)) {
		
			$dt_id = $this->data['HcThuoc']['dtid'];
	//		$dt_id=$this->Cookie->read('old');
			//['DtLoaidt']['doituong_id'];
			
			foreach($this->data['HcThuoc']['hoatchat_id'] as $key=>$value)	{
				$this->HcThuoc->create();
				$record['Thuoc']['id'] = $dt_id ;
				$record['Hoatchat']['id'] = $value;
			
				if (!$this->HcThuoc->saveAll($record)) {
					$this->Session->setFlash(__('Thông tin chưa được lưu. Bạn vui lòng chọn lại', true));
				}
			}
			$this->Session->setFlash(__('Thông tin bạn nhập đã được lưu', true));
			$this->redirect(array('action'=>'index1', $dtid));
		}
		
	/*	$hoatchats = $this->HcThuoc->Hoatchat->find('list');
		$thuocs = $this->HcThuoc->Thuoc->find('list');
		*/
		$this->HcThuoc->recursive = 1;
		$loai = $this->HcThuoc->find('list', array('fields'=>array('HcThuoc.hoatchat_id'),'conditions'=>array('thuoc_id'=>$dtid)));
		//print_r($loai);
		$conditions = array('NOT'=>array('Hoatchat.id'=>$loai));
		$hoatchats = $this->HcThuoc->Hoatchat->find('list',array('fields'=>array('Hoatchat.id','ten_hc'), 'conditions'=>$conditions));
		$this->set(compact('hoatchats', 'thuocs','dtid'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid HcThuoc', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->HcThuoc->save($this->data)) {
				$this->Session->setFlash(__('The HcThuoc has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The HcThuoc could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->HcThuoc->read(null, $id);
		}
		$hoatchats = $this->HcThuoc->Hoatchat->find('list');
		$thuocs = $this->HcThuoc->Thuoc->find('list');
		$this->set(compact('hoatchats','thuocs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for HcThuoc', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->HcThuoc->del($id)) {
			$this->Session->setFlash(__('HcThuoc deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>